#!/usr/bin/env node
const axios     = require('axios');

const cheerio   = require('cheerio');
const isZh      = require('is-chinese');

const {program} = require('commander')
const chalk     = require('chalk')
const urlencode = require('urlencode')

program.version('1.0.0').parse(process.argv); 

// 获取要翻译的内容
const word = process.argv.slice(2).join(' ');
console.log(isZh(word)) 
if(!word){
    process.stdout.write('error: use fy <word or sentence>\n')
    process.exit()
}

// 判定当前你要翻译的地址 以及要 中 -> 英  英文 -> 中文  
let config = {
    url:isZh(word)?'http://www.youdao.com/w/eng/':'http://www.youdao.com/w/',
    language:/^[\u3220-\uFA29]+$/.test(word)?'zh':'en'
}
console.log(config)
let deal = {
    zh($){
        return $('#phrsListTab .trans-container ul p span')
    },
    en($){
        return $('#phrsListTab .trans-container ul li');
    }
}

axios.get(config.url+urlencode(word)).then(res=>{
    let $            = cheerio.load(res.data);
    let results      = deal[config.language]($);
    let printResults = results.text().replace(/\s+/g,' ')+'\n'
    process.stdout.write(chalk.bgGreen(printResults))

}).catch(error=>console.log(error))

